<?php


namespace app\adminapi\business;


use app\adminapi\model\pyg_goods;
use app\adminapi\model\pygSpec;
use app\adminapi\model\PygSpqcValue;
use app\adminapi\model\pygType;
use think\facade\Db;

class TypeBusiness
{
    public static function save($data){
        $type = pygType::create([
            'type_name' => $data['type_name']
        ]);
        $type_id = $type['id'];

        $spec_data = [];
        foreach ($data['spec'] as $v){
            $spec_data[] = [
                'type_id' => $type_id,
                'spec_name' => $v['name'],
                'sort' => $v['sort']
            ];
        }
        $spec = (new pygSpec())->saveAll($spec_data);
        $spec = $spec->toArray();
        $spec = array_column($spec,'id','span_name');
        $spec_value_data = [];
        foreach ($data['spec'] as $v){
            foreach ($v['value'] as $k){
                $spec_value_data = [
                  'type_id' => $type_id,
                  'spec_value' => $v,
                  'spec_id' => $spec[$k['name']]
                ];
            }
        }
        $spec_value = (new PygSpqcValue())->saveAll($spec_value_data);
        dd($spec_value);
    }

    public static function  delete($id){
        // 启动事务
        Db::startTrans();
        try {
            pygType::destroy($id);



            // 提交事务
            Db::commit();
        } catch (\Exception $e) {
            // 回滚事务
            Db::rollback();
        }

    }

    public static function save_goods($param){
        //启动事务
        Db::startTrans();
        try {
//            INSERT INTO `pyg`.`pyg_goods` (`id`, `
//goods_name`, `goods_price`, `market_price`,
// `cost_price`, `goods_number`,
// `frozen_number`, `goods_remark`,
// `goods_desc`, `goods_logo`, `type_id`,
// `brand_id`, `cate_id`, `mould_id`, `weight`,
// `volume`, `keywords`, `comments_num`, `collect_num`,
// `sales_num`, `is_on_sale`, `is_free_shipping`, `is_recommend`,
// `is_new`, `is_hot`, `sort`, `suppliers_id`, `goods_attr`, `create_time`, `update_time`, `delete_time`) VALUES (1, '荣耀10青春版 幻彩渐变 2400万AI自拍 全网通版4GB+64GB 渐变蓝 移动联通电信4G全面屏手机 双卡双待', 5299.00, 5299.00, 5299.00, 100, 0, '', '商品详细信息：<img src=\"/static/home/img/_/intro01.png\" />\n									<img src=\"/static/home/img/_/intro02.png\" />\n									<img src=\"/static/home/img/_/intro03.png\" />', '/uploads/goods/20190528/865565d919219154.jpg', 23, 1, 72, 0, NULL, NULL, '', 0, '0', 0, 1, 1, 1, 1, 1, 50, 0, '[{\"id\":\"7\",\"attr_name\":\"毛重\",\"attr_value\":\"180g\"},{\"id\":\"8\",\"attr_name\":\"产地\",\"attr_value\":\"中国\"}]', 1559054211, 1559054211, NULL);
//
            $goods = [
                'goods_name' => $param['goods_name'],
                'goods_price' => $param['goods_price'],
                'market_price' => $param['market_price'],
                'cost_price' => $param['cost_price'],
                'goods_number' => $param['goods_number'],
                'frozen_number' => $param['frozen_number'],
                'goods_remark' => $param['goods_remark'],
                'goods_desc' => $param['goods_desc'],
                'goods_logo' => $param['goods_logo']
            ];
            // 提交事务s
            Db::commit();
        } catch (\Exception $e) {
            // 回滚事务
            Db::rollback();
        }
    }
}

//array:3 [
//    "type_name" => "手机"
//  "spec" => array:2 [
//    0 => array:3 [
//    "name" => "颜色"
//      "sort" => "50"
//      "value" => array:1 [
//    0 => "黑色"
//]
//    ]
//    1 => array:3 [
//    "name" => "内存"
//      "sort" => "50"
//      "value" => array:1 [
//    0 => "128G"
//]
//    ]
//  ]
//  "attr" => array:2 [
//    0 => array:3 [
//    "name" => "毛重"
//      "sort" => "50"
//      "value" => array:1 [
//    0 => ""
//]
//    ]
//    1 => array:3 [
//    "name" => "产地"
//      "sort" => "50"
//      "value" => array:1 [
//    0 => "国产"
//]
//    ]
//  ]
//]